package com.generator.sysManage.moduleManage.service.impl;

import com.generator.sysManage.moduleManage.dao.IModuleManageMap;
import com.generator.sysManage.moduleManage.pojo.Module;
import com.generator.sysManage.moduleManage.service.IModuleManageService;
import com.generator.sysManage.positionManage.dao.IJMMap;
import com.generator.sysManage.positionManage.pojo.JMJob;
import com.generator.sysManage.positionManage.service.IJMService;
import com.generator.sysManage.sectionManage.pojo.DMDept;
import com.generator.sysManage.sectionManage.service.IDMService;
import com.generator.util.PageModel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * ModuleManageService
 * @author WJW
 * @since  .....
 * @version ......
 */
@Service
@Transactional
public class ModuleManageService implements IModuleManageService {

    @Autowired
    private IModuleManageMap iMMMap;

    public IModuleManageMap getiMMMap() {
        return iMMMap;
    }
    public void setiMMMap(IModuleManageMap iMMMap) {
        this.iMMMap = iMMMap;
    }



    /**
     * 根据条件查询所有模块集合
     * @param module 查询条件
     * @return 模块集合
     */
    public List<Module> getModuleListByJMJob(Module module){
        return iMMMap.getModuleListByModule(module);
    }


    /**
     * 根据条件查询所有模块集合进行分页
     * @param module 查询条件
     * @return pageModel
     */
    public PageModel<Module> getModuleListByFY(Module module,PageModel<Module> pageModel){
        pageModel.setPageNum(pageModel.getPageNum()<=0?1:pageModel.getPageNum());
        pageModel.setPageSize(pageModel.getPageSize()<=0?5:pageModel.getPageSize());
        PageHelper.startPage(pageModel.getPageNum(),pageModel.getPageSize());

        List<Module> userList = iMMMap.getModuleListByModule(module);


        PageInfo<Module> pageInfo = new PageInfo<Module>(userList);

        pageModel.setData(new ArrayList<Module>(userList));
        pageModel.setTotalPage(pageInfo.getPages());
        pageModel.setTotalRows(pageInfo.getTotal());

        return pageModel;
    }



    /**
     * 添加职位
     * @param jmJob 添加的职位信息
     * @return 执行条数
     */
    public Integer addModule(Module jmJob){
        return iMMMap.addModule(jmJob);
    }


    /**
     *进入修改页面
     * @param jmJob 存放module_id 修改时根据module_id进行修改
     * @return 页面路径
     */
    public String updateView(Module jmJob, Model model, HttpSession session){
        session.setAttribute("module_id",jmJob.getModule_id());
        List<Module> dmDeptList = iMMMap.getModuleListByModule(jmJob);
        model.addAttribute("updateModule",dmDeptList.get(0));
        Module module=new Module();
        module.setModule_pid("0");
        List<Module> cityList = iMMMap.getModuleListByModule(module);
        model.addAttribute("moduleList1",cityList);
        return "sys/modules/moduleUpdate";
    }



    /**
     *修改部门
     * @param jmJob 修改的模块信息
     * @return 执行条数
     */
    public Integer updateModule(Module jmJob){
        return iMMMap.updateModule(jmJob);
    }



    /**
     *修改模块状态
     * @param jmJob 修改的模块信息
     * @return 路径
     */
    public String  updateState(Module jmJob, Model model){
        String job_id1 = jmJob.getModule_id();
        String[] split = job_id1.split(",");

        String job_id=split[0];
        Module job=new Module();
        job.setModule_id(job_id);

        int pageNum=Integer.parseInt(split[1]);
        PageModel<Module> pageModel=new PageModel<>();
        pageModel.setPageNum(pageNum);

        if(split[2].equals("0")){
            job.setModule_state("1");
        }else {
            job.setModule_state("0");
        }

        Integer updateState = iMMMap.updateState(job);
        if(updateState==1){
            Module job1=new Module();
            PageModel<Module> deptList = getModuleListByFY(job1,pageModel);
            model.addAttribute("moduleList",deptList);
            return "sys/modules/moduleList";
        }
        return "forward:/moduleManage/moduleList";
    }


}
